package ca.llsutherland.squash.domain.helper;

import ca.llsutherland.squash.domain.Administrator;
import ca.llsutherland.squash.domain.Clock;
import ca.llsutherland.squash.domain.Role;
import ca.llsutherland.squash.exceptions.ValidationException;
import ca.llsutherland.squash.role.RolesValidator;
import ca.llsutherland.squash.utils.ErrorConstants;

public class AdministratorsHelper {
    
    public static Administrator createUnpersistedAdministrator(String firstname, String lastname, Clock dateCreated, Clock dateUpdated) {
        Administrator administrator = new Administrator();
        administrator.setFirstname(firstname);
        administrator.setLastname(lastname);
        administrator.setDateCreated(dateCreated);
        administrator.setDateUpdated(dateUpdated);
        return administrator;
    }

    public static Administrator createUnpersistedAdministrator(String firstname, String lastname, Clock dateCreated, Clock dateUpdated, String password) {
        Administrator administrator = createUnpersistedAdministrator(firstname, lastname, dateCreated, dateUpdated);
        administrator.setPassword(password);
        return administrator;
    }
    
    public static Administrator createUnpersistedAdministrator(String firstname, String lastname, Clock dateCreated, Clock dateUpdated, String password, Role role) {
        Administrator administrator = createUnpersistedAdministrator(firstname, lastname, dateCreated, dateUpdated, password);
        administrator.setRole(role);
        return administrator;
    }

    public static Administrator createNewAdministrator(String firstname, String lastname, Clock dateCreated, Clock dateUpdated, String password, Role role, Long id) {
        Administrator administrator = createUnpersistedAdministrator(firstname, lastname, dateCreated, dateUpdated, password, role);
        administrator.setId(id);
        return administrator;
    }
    

    public static void assertValidAdministrator(Administrator administrator) {
        DomainObjectHelper.assertValidId(administrator.getId());
        RolesValidator.assertRole(administrator.getRole());
    }

    public static void assertNewValid(Administrator administrator) {
        if(administrator == null) {
            throw new ValidationException(ErrorConstants.NULL_ADMINISTRATOR_ERROR);
        }
        
        DomainObjectHelper.assertInvalidStringWithErrorMessage(administrator.getFirstname(), ErrorConstants.INVALID_FIRST_NAME_ERROR);
        DomainObjectHelper.assertInvalidStringWithErrorMessage(administrator.getLastname(), ErrorConstants.INVALID_LAST_NAME_ERROR);
        DomainObjectHelper.assertNonBlankStringWithErrorMessage(administrator.getPassword(), ErrorConstants.INVALID_PASSWORD_ERROR);
        
        if(administrator.getRole() == null) {
            throw new ValidationException(ErrorConstants.MISSING_ROLE_ERROR);
        }
        
        RolesHelper.assertValidRole(administrator.getRole());
    }



}
